Skip to content

feat(vm): optimize account energy usage window reset (TIP-945) - #7119

Merged
yanghang8612 merged 1 commit into
tronprotocol:release_v4.8.3from
yanghang8612:opt/reset-account-usage-v2
Nov 23, 2026
Merged

yanghang8612 merged 1 commit into
tronprotocol:release_v4.8.3from
yanghang8612:opt/reset-account-usage-v2

Conversation

@yanghang8612

Copy link
Copy Markdown
Collaborator

What does this PR do?

Refine account energy usage window reset under allowTvmResourceOptimization (TIP-945):

  • TransactionTrace.java:
    • In resetAccountUsage (V1) and resetAccountUsageV2 (V2), evaluate areaMerged as currentUsage > mergedUsage || mergedSize != currentSize when dynamicPropertiesStore.allowTvmResourceOptimization() is enabled (preserving mergedSize != currentSize before activation).
    • Under allowTvmResourceOptimization(), validate via checkResetEnergyUsage(newUsage) that newUsage does not exceed 2 * totalEnergyCurrentLimit, throwing ArithmeticException otherwise.
  • Tip945VmTest.java:
    • Add unit tests covering V1/V2 energy window retention for caller and origin accounts, window preservation on UNDELEGATERESOURCE, and upper bound validation before and after proposal activation.

Why are these changes required?

When energy usage increases during VM execution while the active window size matches the pre-consumed window size, retaining the active window keeps account energy window accounting consistent after proposal activation. Adding the upper bound check under allowTvmResourceOptimization guards against out-of-range reset energy usage without affecting pre-proposal behavior or public method signatures.

This PR has been tested by:

  • Unit Tests:
    • ./gradlew :framework:test --tests "org.tron.core.vm.nativecontract.Tip945VmTest" (14/14 passed).
    • ./gradlew :framework:test --tests "org.tron.core.db.TransactionTraceTest" (6/6 passed).
  • Static Checks: ./gradlew :framework:checkstyleMain :framework:checkstyleTest :plugins:checkstyleMain and bash .github/scripts/check_math_usage.sh passed with 0 violations.
  • Environment: Amazon Corretto 17 on macOS ARM64 (RocksDB).

Follow up

None.

Extra details

Strictly gated behind allowTvmResourceOptimization (TIP-945); pre-proposal behavior remains unchanged.

@utafrali utafrali left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The logic change is well-reasoned and correctly gated behind the feature flag, with solid test coverage for V1/V2 paths and boundary conditions. The main practical concern is the double allowTvmResourceOptimization() DB read per method call; the other two comments are clarifications around an edge-case boundary and a non-obvious test assertion.

// If area merging happened during suicide, use the current window size
long newSize = mergedSize == currentSize ? size : currentSize;
boolean areaMerged = dynamicPropertiesStore.allowTvmResourceOptimization()
? currentUsage > mergedUsage || mergedSize != currentSize

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

allowTvmResourceOptimization() is called twice per method invocation (once for areaMerged, once for the guard before checkResetEnergyUsage). If this method reads from the dynamic properties store (a DB-backed map), that is two unnecessary lookups per call. Cache it as a local boolean at the top of each method:

boolean isOptimizationAllowed = dynamicPropertiesStore.allowTvmResourceOptimization();
boolean areaMerged = isOptimizationAllowed
    ? currentUsage > mergedUsage || mergedSize != currentSize
    : mergedSize != currentSize;
...
if (isOptimizationAllowed) {
  checkResetEnergyUsage(newUsage);
}

Same applies in resetAccountUsageV2.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch — cached dynamicPropertiesStore.allowTvmResourceOptimization() in a local boolean allowOptimization variable in both resetAccountUsage and resetAccountUsageV2 to avoid the redundant lookup. Updated in c598184.

long maxUsage = totalEnergyCurrentLimit <= 0 ? 0L
: (totalEnergyCurrentLimit > Long.MAX_VALUE / 2
? Long.MAX_VALUE : totalEnergyCurrentLimit * 2);
if (newUsage > maxUsage) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When totalEnergyCurrentLimit <= 0, maxUsage is set to 0L, which means any newUsage > 0 immediately throws. This could be a subtle footgun if the store ever returns 0 (e.g. during early initialization or a misconfiguration). Consider whether the intent is really "no limit" (skip the check) or "all usage is invalid" (throw). If the intent is a safety guard, a comment explaining the invariant that makes totalEnergyCurrentLimit <= 0 impossible in practice would prevent future confusion. Otherwise, guard with a no-op:

if (totalEnergyCurrentLimit <= 0) {
  return; // limit not yet set, skip check
}

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Keeping maxUsage = 0L when totalEnergyCurrentLimit <= 0 is intentional here:

  • TOTAL_ENERGY_CURRENT_LIMIT is always initialized at genesis (50,000,000,000L by default; reading an uninitialized entry throws IllegalArgumentException), so it cannot silently default to 0 during startup.
  • If governance explicitly sets TOTAL_CURRENT_ENERGY_LIMIT to 0 (or if a non-positive value were ever encountered), keeping maxUsage = 0L preserves the literal 2 * totalEnergyCurrentLimit bound (2 * 0 = 0) rather than bypassing the check or multiplying a negative value.


for (long initialUsage : new long[]{overLimitUsage, cap, withinCapUsage}) {
setEnergyAccount(CALLER, 10_000L * TRX_PRECISION, initialUsage, 20_000L,
cancelAllV2 == 1);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The else branch asserts assertEquals(initialUsage, account(CALLER).getEnergyUsage()) after finishing a trace that runs a real contract. This passes only if the trivial bytecode "60015000" leaves CALLER's stored energy usage unchanged by resetAccountUsage. That is a non-obvious postcondition. A short comment explaining why (e.g. the contract consumes no stake-based energy so newUsage rounds back to initialUsage) would make the assertion self-documenting and prevent false confidence if the test helper is reused with a different contract.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point — initialUsage (150_000_000L / 200_000_000L) is well above CALLER's frozen energy limit (from 10,000 TRX), so leftFrozenEnergy is 0 and the "60015000" execution pays its small bill entirely in TRX without consuming staked energy, leaving CALLER's stored energyUsage unchanged at initialUsage. Keeping the test minimal as-is since this fixture only runs that trivial bytecode — thanks for the careful review!

@yanghang8612
yanghang8612 force-pushed the opt/reset-account-usage-v2 branch from 77f9a17 to 47a909a Compare November 23, 2026 03:44
Under allowTvmResourceOptimization (TIP-945), refine TransactionTrace.resetAccountUsage and resetAccountUsageV2:
- Retain the active window size when energy usage increases during execution (currentUsage > mergedUsage) in addition to window size changes (mergedSize != currentSize).
- Validate that the reset energy usage does not exceed 2 * totalEnergyCurrentLimit, throwing ArithmeticException otherwise.

Add unit tests in Tip945VmTest covering V1/V2 window reset behavior and upper bound validation.
@yanghang8612
yanghang8612 force-pushed the opt/reset-account-usage-v2 branch from 47a909a to c598184 Compare November 23, 2026 04:20
@yanghang8612
yanghang8612 merged commit 171bb6e into tronprotocol:release_v4.8.3 Nov 23, 2026
10 checks passed
@halibobo1205 halibobo1205 added the topic:vm VM, smart contract label Nov 23, 2026
@halibobo1205 halibobo1205 added this to the GreatVoyage-v4.8.3 milestone Nov 23, 2026
@yanghang8612
yanghang8612 deleted the opt/reset-account-usage-v2 branch November 23, 2026 08:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

topic:vm VM, smart contract

Projects

None yet

Development

Successfully merging this pull request may close these issues.

8 participants